package com.wanpinghui.supplier.addrbook.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wanpinghui.supplier.R;
import com.wanpinghui.wphlibrary.model.addrbook.bean.MoblieBean;
import com.wanpinghui.wphlibrary.widgets.ThrottleButton;
import com.yunshl.yunshllibrary.recyclerview.BaseRecyclerViewAdapter;
import com.yunshl.yunshllibrary.utils.TextUtil;

/**
 * 功能描述
 * author:caitiangui
 * time:2017/12/20
 */

public class AddressBooksAdapter extends BaseRecyclerViewAdapter<MoblieBean, RecyclerView.ViewHolder> {
    public static final int TYPE_HEADER = -1;
    public static final int TYPE_NORMAL = 1;
    private boolean b = false;

    private boolean isNewFriends;

    private boolean isMayKnow;

    private boolean addFriendsLog;

    private boolean friends;

    private View mHeaderView;

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    private int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    private setOnItemListener listener;

    public AddressBooksAdapter(Context context) {
        super(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER)
            return new addrbookHeaderViewHolder(mHeaderView);
        View view = LayoutInflater.from(context).inflate(R.layout.item_addressbooks, parent, false);
        return new AddressBookViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        super.onBindViewHolder(holder, position);
        if (getItemViewType(position) != TYPE_HEADER) {
         final    AddressBookViewHolder addressBookViewHolder = (AddressBookViewHolder) holder;
            final MoblieBean bean = datas.get(getRealPosition(holder));
            if (isNewFriends()) {
                switch (bean.getState()) {

                    case 10: //可邀请
                        setbtnStatus(addressBookViewHolder, "邀请Ta");
                        break;
                    case 20://可添加好友
                        setbtnStatus(addressBookViewHolder, "+好友");
                        break;
                    default:
                        setStatus(addressBookViewHolder, bean.getStatus_display());
                        break;
                }
            } else if (isAddFriendsLog()) {
                switch (bean.getStatus()) {

                    case 10: //已邀请
                        setbtnStatus(addressBookViewHolder, "邀请Ta");
                        break;
                    case 20://等待认证
                        if (bean.isIs_followed()) {  // true 被添加（等待验证的状态时显示通过验证按钮）  false 添加别人
                            setbtnStatus(addressBookViewHolder, "通过认证");
                        } else {
                            setStatus(addressBookViewHolder, bean.getStatus_display());
                        }
                        break;
                    default://已添加
                        setStatus(addressBookViewHolder, bean.getStatus_display());
                        break;


                }
            }
            Glide.with(context).load(bean.getAvatar()).placeholder(R.mipmap.my_icon_head_default).into(addressBookViewHolder.iv_head);
            if (isMayKnow) { // 可能认识
                if (TextUtil.isNotEmpty(bean.getCom())) {
                    addressBookViewHolder.tv_company_name.setText(bean.getCom());
                } else {
                    addressBookViewHolder.tv_company_name.setText("个体工程商");
                }

                addressBookViewHolder.tv_common_people.setVisibility(View.VISIBLE);
                if (bean.getShared_friends_count() > 0) {
                    addressBookViewHolder.tv_common_people.setText(bean.getShared_friends_count() + "位共同好友");
                } else {
                    addressBookViewHolder.tv_common_people.setText("通讯录好友");
                }
            } else if (isAddFriendsLog()) {  // 添加好友记录
                if (TextUtil.isNotEmpty(bean.getCom())) {
                    addressBookViewHolder.tv_company_name.setText(bean.getCom());
                } else {
                    addressBookViewHolder.tv_company_name.setText("个体工程商");
                }
            } else if (isFriends()) {  // 生意好友
                if (TextUtil.isNotEmpty(bean.getCom())) {
                    addressBookViewHolder.tv_company_name.setText(bean.getCom());
                } else {
                    addressBookViewHolder.tv_company_name.setText("个体工程商");
                }
            } else {   //  通讯录列表
                addressBookViewHolder.tv_common_people.setVisibility(View.GONE);
                addressBookViewHolder.tv_company_name.setText(bean.getMobile());
            }

            addressBookViewHolder.tv_name.setText(bean.getNickname());
            addressBookViewHolder.btn_submit.setOnThrottleClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (addressBookViewHolder.btn_submit.getText().toString().contains("好友")) {
                        if (listener != null) {
                            listener.setAddFriend(position);
                        }
                    } else if (addressBookViewHolder.btn_submit.getText().toString().contains("通过")) {
                        if (listener != null) {
                            listener.setAgreeFriend(position);
                        }
                    } else {
                        if (listener != null) {
                            listener.setInvite(position);
                        }
                    }

                }
            });

        }
    }

    private void setbtnStatus(AddressBookViewHolder holder, String string) {
        holder.btn_submit.setVisibility(View.VISIBLE);
        holder.tv_status.setVisibility(View.GONE);
        holder.btn_submit.setText(string);
    }

    private void setStatus(AddressBookViewHolder holder, String string) {
        holder.btn_submit.setVisibility(View.GONE);
        holder.tv_status.setVisibility(View.VISIBLE);
        holder.tv_status.setText(string);
    }

    @Override
    public int getItemCount() {
        if (datas != null) {
            if (mHeaderView != null) {
                return datas.size() + 1;
            } else {
                return datas.size();
            }
        } else {
            return 0;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null) return TYPE_NORMAL;
        if (position == 0) return TYPE_HEADER;
        return TYPE_NORMAL;
    }


    public boolean isNewFriends() {
        return isNewFriends;
    }

    public void setNewFriends(boolean newFriends) {
        isNewFriends = newFriends;
    }

    public void setListener(setOnItemListener listener) {
        this.listener = listener;
    }

    public boolean isMayKnow() {
        return isMayKnow;
    }

    public void setMayKnow(boolean mayKnow) {
        isMayKnow = mayKnow;
    }

    public boolean isAddFriendsLog() {
        return addFriendsLog;
    }

    public void setAddFriendsLog(boolean addFriendsLog) {
        this.addFriendsLog = addFriendsLog;
    }

    public boolean isFriends() {
        return friends;
    }

    public void setFriends(boolean friends) {
        this.friends = friends;
    }


    class AddressBookViewHolder extends RecyclerView.ViewHolder {
        ImageView iv_head;
        TextView tv_name;
        TextView tv_company_name;
        TextView tv_status;
        ThrottleButton btn_submit;

        TextView tv_common_people;

        AddressBookViewHolder(View itemView) {
            super(itemView);
            tv_common_people = (TextView) itemView.findViewById(R.id.tv_common_people);
            iv_head = (ImageView) itemView.findViewById(R.id.iv_head);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_company_name = (TextView) itemView.findViewById(R.id.tv_company_name);
            tv_status = (TextView) itemView.findViewById(R.id.tv_status);
            btn_submit = (ThrottleButton) itemView.findViewById(R.id.btn_submit);
        }
    }

    class addrbookHeaderViewHolder extends RecyclerView.ViewHolder {
        public addrbookHeaderViewHolder(View itemView) {
            super(itemView);
        }
    }

    public interface setOnItemListener {
        void setAddFriend(int position);

        void setAgreeFriend(int position);

        void setInvite(int position);
    }

}
